<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增方法')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-method-add">
        <input type="hidden" name="serId" th:value="${serId}">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">名称：</label>
            <div class="col-sm-8">
                <input id="modName" name="modName" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label" >描述：</label>
            <div class="col-sm-8">
                <input name="modDes" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">模板：</label>
            <div class="col-sm-8">
                <select id="template" name="templateName" class="form-control selectpicker" data-live-search="true" data-none-selected-text=" ">
                    <option></option>
                    <option th:each="template:${templates}" th:data="${template.temId}" th:value="${template.temName}" th:text="${template.temName}" th:disabled="${template.temStatus == '1'}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">实验参数（部分）：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="acqupara" class="form-control" type="text" placeholder="修改实验参数" readonly>
                    <span class="input-group-addon" title="恢复默认参数值" onmousedown="toAjax($('#template option:selected').attr('data'))"><i class="fa fa-refresh"></i></span>
                </div>
                <div class="ms-parent" style="width: 100%;">
                    <div class="acqupara-drop animated flipInX" style="max-height:200px;overflow-y:auto">

                    </div>
                </div>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可修改实验参数在模板文件的autopara.xml中定义, 推荐使用默认值</span>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/method";
    var serId = [[${serId}]];

    $("#form-method-add").validate({
        onkeyup: false,
        rules: {
            modName: {
                remote: {
                    url: prefix + "/checkModNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "modName": function () {
                            return $.common.trim($("#modName").val());
                        },
                        "serId": serId
                    }
                }
            }
        },
        messages: {
            "modName": {
                remote: "该方法已经存在",
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        var value = $("#template option:selected").attr("data");
        if($.common.isEmpty(value)){
            $.modal.alertError("请选择模板");
            return false;
        }
        if ($.validate.form()) {
            var data = $('#form-method-add').serializeArray();
            var param = [];
            $('.acqupara-drop').find("input").each(function (key, value, index) {
                var val = $(this).val();
                var name = $(this).attr("name");
                if($.common.isNotEmpty(name)){
                    param.push(name + "=" + val);
                }
            })
            data.push({"name": "modAcqupara", "value": param.join('|')});
            $.operate.save(prefix + "/add", data);
        }
    }

    $("input[name='approveTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    })

    $("#template").change(function () {
        var value = $("#template option:selected").attr("data");
        if($.common.isNotEmpty(value)){
            toAjax(value)
        }
    })

    function toAjax(value){
        $(".acqupara-drop").empty();
        $.ajax({
            type: "post",
            url: prefix + "/selectTemplate",
            data: {
                "temId": value
            },
            success: function(data) {
                if(data.code == web_status.SUCCESS){
                    if($.common.isNotEmpty(data.data)){
                        var param = data.data.group.param;
                        var html = [];
                        $.each(param, function (key, value, index) {
                            var label = '<label class="col-sm-3 control-label">'+value.name+': </label>';
                            var type = 'text';
                            if (value.type == "real"){
                                //double
                                type = 'number';
                            }else if(value.type == "string"){
                                //string
                                type = 'text';
                            }else if(value.type == "integer"){
                                //int
                                type = 'number';
                            }else {
                                //other
                                type = 'text';
                            }
                            var input = '<div class="col-sm-8"><input class="form-control" type="'+type+'" name="'+value.name+'" value="'+value.value+'" min="'+value.min+'" max="'+value.max+'"></div>';
                            html.push(label);
                            html.push(input);
                        })
                        if(param.length > 0){
                            $(".acqupara-drop").append(html);
                        }
                    }
                }else {
                    top.layer.alert(data.msg, {
                        icon: $.modal.icon(modal_status.FAIL),
                        title: "错误提示",
                        btn: ['确认'],
                        btnclass: ['btn btn-primary'],
                    });
                }

            }
        });
    }
</script>
</body>
</html>